<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.userId"
          placeholder="请输入用户id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>

        <el-date-picker
          v-model="searchParams.startTime"
          type="datetime"
          placeholder="选择开始时间"
          size="mini"
        ></el-date-picker>
        <el-date-picker
          v-model="searchParams.endTime"
          type="datetime"
          placeholder="选择结束时间"
          size="mini"
        ></el-date-picker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('challengeBoxOrders/index')"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('challengeBoxOrders/index')"
          >重置</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          prop="order_no"
          label="订单号"
          align="center"
          fixed
        ></el-table-column>
        <el-table-column label="用户信息" align="center" width="200px">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                fit="contain"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="擂台信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <el-image
                :src="scope.row.challenge_box_image"
                :preview-src-list="[scope.row.challenge_box_image]"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px'
                }"
              ></el-image>
              <div>
                <div>{{ scope.row.challenge_box_name }}</div>
                <div>￥{{ scope.row.challenge_box_price }}</div>
              </div>
            </div>
          </template>
        </el-table-column>

<!--        <el-table-column prop="pay_mode" label="模式" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <span v-if="scope.row.pay_mode == 1">单抽</span>-->
<!--            <span v-if="scope.row.pay_mode == 2">3连抽</span>-->
<!--            <span v-if="scope.row.pay_mode == 3">5连抽</span>-->
<!--            <span v-if="scope.row.pay_mode == 4">10连抽</span>-->
<!--            <span v-if="scope.row.pay_mode == 5">30连抽</span>-->
<!--            <span v-if="scope.row.pay_mode == 6">50连抽</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column
          prop="num"
          label="数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="multiple"
          label="倍数"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="total_price"
          label="总价格"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="pay_price"
          label="实付款"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="discount_price"
          label="折扣"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="prize(scope.row.id)"
              v-if="has('challengeBoxOrders/prize')"
              >获得商品</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>

    <el-card class="box-card" v-if="status === 'prize'">
      <div slot="header">
        <el-page-header @back="back()" content="订单商品"></el-page-header>
      </div>
      <el-table
        :data="prizeData.data"
        v-loading="prizeTableLoading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column label="赏品信息" align="center" width="300px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <el-image
                :src="scope.row.challenge_box_prize_image"
                :preview-src-list="[scope.row.challenge_box_prize_image]"
                fit="contain"
                :style="{
                  width: '50px',
                  marginRight: '10px'
                }"
              ></el-image>
              <div>
                <div>{{ scope.row.challenge_box_prize_name }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_name" label="等级" align="center">
          <template slot-scope="scope">
            <span
              :style="{
                backgroundColor: scope.row.level_color,
                color: '#fff',
                padding: '10px',
                height: '20px',
                lineHeight: '20px',
                textAlign: 'center',
                width: '100px',
                borderRadius: '10px'
              }"
            >
              {{ scope.row.level_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="num"
          label="数量"
          align="center"
          key="546544169"
        ></el-table-column>
        <el-table-column
          prop="price"
          label="价值"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="recovery_price"
          label="兑换价"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      prizeData: [], // 表格诗句
      table_loading: true, // 表格加载中
      prizeTableLloading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        page: 1,
        limit: 10,
        userId: "",
        startTime: "",
        endTime: ""
      }
    };
  },
  created() {
    this.getData();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    back() {
      this.status = "index";
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.challengeBoxOrders
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.userId = "";
      this.searchParams.startTime = "";
      this.searchParams.endTime = "";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    prize(id) {
      this.status = "prize";
      this.prizeTableLoading = true;
      api.challengeBoxOrders
        .prize({ params: { box_order_id: id } })
        .then(res => {
          if (res.data.code == 200) {
            this.prizeTableLoading = false;
            this.prizeData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
